
#include "PreHeader.h"
#include "cLogicDataManager.h"

cLogicDataManager* cLogicDataManager::it()
{
	static cLogicDataManager logicManager;
	return &logicManager;
}

cLogicDataManager::cLogicDataManager()
{

}

cLogicDataManager::~cLogicDataManager()
{

}

sResult cLogicDataManager::Load()
{
	// read constant
	tString path = CCFileUtils::fullPathFromRelativePath("constant.constant");
	sResult res = LoadConstant(path);
	RETURN_FAIL_RESULT(res);

	// read food 
	path = CCFileUtils::fullPathFromRelativePath("food.food");
	res = LoadFood(path);
	RETURN_FAIL_RESULT(res);

	// read stage
	path = CCFileUtils::fullPathFromRelativePath("stage.stage");
	res = LoadStage(path);
	RETURN_FAIL_RESULT(res);

	return eResultCode::OK;
}

const nConstantFormat01::sConstant* cLogicDataManager::GetConstantData()
{
	return &m_constantData;
}

const nFoodFormat01::sFood* cLogicDataManager::GetFoodData(tUInt ID)
{
	tFoodDataMap::iterator it = m_foodDataMap.find(ID);
	if (it == m_foodDataMap.end())
	{
		return NULL;
	}

	return &(it->second);
}

const nStageFormat01::sStage* cLogicDataManager::GetStageData(tUInt ID)
{
	tStageDataMap::iterator it = m_stageDataMap.find(ID);
	if (it == m_stageDataMap.end())
	{
		return NULL;
	}

	return &(it->second);
}

sResult cLogicDataManager::LoadConstant(const tFilename& filename)
{
	cConstantFormatReader reader;
	sResult res = reader.Read(filename, this);
	RETURN_FAIL_RESULT(res);

	return eResultCode::OK;
}

sResult cLogicDataManager::LoadFood(const tFilename& filename)
{
	cFoodFormatReader reader;
	sResult res = reader.Read(filename, this);
	RETURN_FAIL_RESULT(res);

	return eResultCode::OK;
}

sResult cLogicDataManager::LoadStage(const tFilename& filename)
{
	cStageFormatReader reader;
	sResult res = reader.Read(filename, this);
	RETURN_FAIL_RESULT(res);

	return eResultCode::OK;
}

sResult cLogicDataManager::OnReadConstantFormat(const nConstantFormat01::sConstant& constant)
{
	m_constantData = constant;

	return eResultCode::OK;
}

sResult cLogicDataManager::OnReadFoodFormat(const nFoodFormat01::sFood& food)
{
	m_foodDataMap.insert(std::make_pair(food.ID, food));

	return eResultCode::OK;
}

sResult cLogicDataManager::OnReadStageFormat(const nStageFormat01::sStage& stage)
{
	m_stageDataMap.insert(std::make_pair(stage.ID, stage));

	return eResultCode::OK;
}